package cn.innosmart.aq.customize.datetimepicker;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import cn.innosmart.aq.R;


/**
 * Created by David on 2016/1/20.
 */
public class WeekChooseView extends LinearLayout {
    private static OnRepeatStatusChanged onRepeatStatusChanged;
    private TextViewWithCircularIndicator cbMondeay;
    private TextViewWithCircularIndicator cbTusday;
    private TextViewWithCircularIndicator cbWensday;
    private TextViewWithCircularIndicator cbThursday;
    private TextViewWithCircularIndicator cbFriday;
    private TextViewWithCircularIndicator cbSaturday;
    private TextViewWithCircularIndicator cbSunday;
    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isChecked = false;
            if (v instanceof TextViewWithCircularIndicator) {
                isChecked = !((TextViewWithCircularIndicator) v).isChecked();
            }
            switch (v.getId()) {
                case R.id.week_monday:
                    cbMondeay.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(1, isChecked);
                    break;
                case R.id.week_tuesday:
                    cbTusday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(2, isChecked);
                    break;
                case R.id.week_wensday:
                    cbWensday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(3, isChecked);
                    break;
                case R.id.week_thursday:
                    cbThursday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(4, isChecked);
                    break;
                case R.id.week_friday:
                    cbFriday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(5, isChecked);
                    break;
                case R.id.week_saturday:
                    cbSaturday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(6, isChecked);
                    break;
                case R.id.week_sunday:
                    cbSunday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(0, isChecked);
                    break;
            }
        }
    };

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()) {
                case R.id.week_monday:
                    cbMondeay.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(1, isChecked);
                    break;
                case R.id.week_tuesday:
                    cbTusday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(2, isChecked);
                    break;
                case R.id.week_wensday:
                    cbWensday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(3, isChecked);
                    break;
                case R.id.week_thursday:
                    cbThursday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(4, isChecked);
                    break;
                case R.id.week_friday:
                    cbFriday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(5, isChecked);
                    break;
                case R.id.week_saturday:
                    cbSaturday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(6, isChecked);
                    break;
                case R.id.week_sunday:
                    cbSunday.setChecked(isChecked);
                    if (onRepeatStatusChanged != null)
                        onRepeatStatusChanged.onCheckChanged(0, isChecked);
                    break;
            }
        }
    };

    public WeekChooseView(Context context) {
        this(context, null, 0);
    }

    public WeekChooseView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public WeekChooseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.week_choose, this, true);
        assignViews();
        assignListeners();
    }

    public void reset() {
        cbSunday.setChecked(false);
        cbMondeay.setChecked(false);
        cbTusday.setChecked(false);
        cbWensday.setChecked(false);
        cbThursday.setChecked(false);
        cbFriday.setChecked(false);
        cbSaturday.setChecked(false);
        invalidate();
    }

    public void setChecked(int position, boolean isChecked) {
        switch (position) {
            case 0:
                cbSunday.setChecked(isChecked);
                break;
            case 1:
                cbMondeay.setChecked(isChecked);
                break;
            case 2:
                cbTusday.setChecked(isChecked);
                break;
            case 3:
                cbWensday.setChecked(isChecked);
                break;
            case 4:
                cbThursday.setChecked(isChecked);
                break;
            case 5:
                cbFriday.setChecked(isChecked);
                break;
            case 6:
                cbSaturday.setChecked(isChecked);
                break;
        }
    }

    public void getWeeks() {

    }

    private void assignListeners() {
        cbMondeay.setOnClickListener(onClickListener);
        cbTusday.setOnClickListener(onClickListener);
        cbWensday.setOnClickListener(onClickListener);
        cbThursday.setOnClickListener(onClickListener);
        cbFriday.setOnClickListener(onClickListener);
        cbSaturday.setOnClickListener(onClickListener);
        cbSunday.setOnClickListener(onClickListener);
    }

    private void assignViews() {
        cbMondeay = (TextViewWithCircularIndicator) findViewById(R.id.week_monday);
        cbTusday = (TextViewWithCircularIndicator) findViewById(R.id.week_tuesday);
        cbWensday = (TextViewWithCircularIndicator) findViewById(R.id.week_wensday);
        cbThursday = (TextViewWithCircularIndicator) findViewById(R.id.week_thursday);
        cbFriday = (TextViewWithCircularIndicator) findViewById(R.id.week_friday);
        cbSaturday = (TextViewWithCircularIndicator) findViewById(R.id.week_saturday);
        cbSunday = (TextViewWithCircularIndicator) findViewById(R.id.week_sunday);
    }

    public void setOnRepeatStatusChanged(OnRepeatStatusChanged onRepeatStatusChanged) {
        this.onRepeatStatusChanged = onRepeatStatusChanged;
    }

    public interface OnRepeatStatusChanged {
        void onCheckChanged(int position, boolean isChecked);
    }

}
